package units.villagers;

///* ----------------------------------------------------------------------
// *	Shadow Quest
// *	Copyright (C) 2009 Prageeth Silva <prageeth@thenewcoders.com>
// *
// *  This program is free software: you can redistribute it and/or modify
// *  it under the terms of the GNU General Public License as published by
// *  the Free Software Foundation, either version 3 of the License, or
// *  (at your option) any later version.
// *
// *  This program is distributed in the hope that it will be useful,
// *  but WITHOUT ANY WARRANTY; without even the implied warranty of
// *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// *  GNU General Public License for more details.
// *
// *  You should have received a copy of the GNU General Public License
// *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
// *  ----------------------------------------------------------------------
// */
//
//package units.villagers;
//
//import map.Item;
//import map.World;
//import map.Item.ItemType;
//
//import org.newdawn.slick.Animation;
//import org.newdawn.slick.Image;
//import org.newdawn.slick.Input;
//import org.newdawn.slick.SlickException;
//import org.newdawn.slick.SpriteSheet;
//
//import sq.RPG;
//import units.Player;
//import units.Unit;
//import units.Unit.AttackType;
//import units.Unit.UnitType;
//import units.allies.Ally;
//
///** Represents the Villager characters
// * 
// */
//public class CopyOfVillager extends Unit
//{
//	
//	// the type of the villager
//	public enum VillagerType { PRINCE_ALDRIC, ELVIRA, GARTH, UNICORN }
//	
//	private Animation healLeft, healRight, healing;
//	private VillagerType type;
//	
//    /** Create a new Villager object.
//     * @param type
//     * @param x
//     * @param y
//     * @throws SlickException
//     */
//    public CopyOfVillager(VillagerType type, int width, int height)
//    throws SlickException
//    {
//    	this(type,width,height,1);
//    }
//    
//	/** Create a new Villager object (overload).
//	 * @param type The type of the villager
//	 * @param x The x coordinate in of the villager
//	 * @param y The y coordinate in of the villager
//	 * @param maxHP The maximum HP of the villager
//	 * @throws SlickException
//	 */
//    public CopyOfVillager(VillagerType type, int width, int height, int maxHP)
//    throws SlickException
//    {
//    	// temporarily make the super class and then replace the images
//    	super(Unit.UnitType.VILLAGER,width,height,maxHP);
//    	this.setDialogueTimer(DIALOG_INTERVAL);
//       	this.setVillagerType(type);
//       	this.setKillable(false);
//    	
//       	String dirPath = "";
//       	Image img;
//    	switch (type) {
//			case PRINCE_ALDRIC:
//				dirPath = "assets/units/villagers/prince/";
//				this.setName("Prince Aldric");
//				break;
//	 		case ELVIRA:
//	 			dirPath = "assets/units/villagers/elvira/";
//	 			img = new Image(dirPath+"heal_left.png");
//				this.setHealLeftAnimation(img);
//				this.setHealRightAnimation(img.getFlippedCopy(true, false));
//				this.setHealUnitAnimation(new Image(dirPath+"healhalo.png"));
//				this.setName("Elvira");
//				break;
//	  		case GARTH:
//	  			dirPath = "assets/units/villagers/garth/";
//	  			this.setName("Garth");
//				break; 
//	  		case UNICORN:
//	 			dirPath = "assets/units/villagers/unicorn/";
//	 			img = new Image(dirPath+"heal_left.png");
//				this.setHealLeftAnimation(img);
//				this.setHealRightAnimation(img.getFlippedCopy(true, false));
//				this.setHealUnitAnimation(new Image(dirPath+"healhalo.png"));
//				this.setName("Unicorn");
//				break;
//    	}
//    	
//    	this.setAttackType(AttackType.UNATTACKABLE);
//    	this.loadImages(dirPath);  	
//    	this.standInRandomDirection();
//    	
//    }
//    
//    /** Update the player's state for a frame.
//     * @param world A reference to the world.
//     * @param input The input object, used to take the keyboard inputs.
//     * @param delta Time passed since last frame (milliseconds).
//     */
//    public void update(World world, Input input, int delta) {
//    
//    	if (this.isAlive()) {
//    		
//    		Player player = world.getPlayer();
//    		
//	    	double dist2 = calculateDistanceSqrd(player.getX(), player.getY(), this.getX(), this.getY());
//	    	
//	    	// makes sure the dialog is shown only for a limited time
//	    	if (this.isShowUnitDialogue()) {
//		     	this.setDialogueTimer(this.getDialogueTimer()-delta);
//		     	if ((this.getDialogueTimer() <= 0) || (dist2 > Unit.Squared(VISIBLITY))) {
//		     		this.setDialogueTimer(DIALOG_INTERVAL);
//		     		this.hideDialogue();
//		     	}
//	    	}
//	    	
//	    	// make the villager look at the player when interacting with the player
//	    	if (!player.isTeleporting() && player.isAlive() && (dist2 <= Unit.Squared(VISIBLITY))) {
//	    		double distx = player.getX()-this.getX();
//	    		if (player.getY() < this.getLowerY()) { this.lookUp(); }
//	    		else if (distx < 0) { this.lookLeft(); }
//	    		else if (distx > 0 ) { this.lookRight(); }
//	    	} else {
//	    		this.idle(delta);
//	    	}
//	     	
//	     	switch (this.getVillagerType()) {
//				case PRINCE_ALDRIC:
//					if (!player.isTeleporting() && player.isAlive() && (dist2 <= Unit.Squared(CLOSE_PROXIMITY))) {
//						Item i = Item.searchItem(player,Item.ItemType.ELIXIR);
//						if ((i != null) || (Item.searchItem(this,Item.ItemType.ELIXIR) != null)) {
//							this.setDialogue("The elixir! My father is cured! Thankyou!");
//							if (i != null) { player.giveItem(i,this); }
//							RPG.winGame();
//						} else {
//							this.setDialogue("Please seek out the Elixir of Life to cure the king.");
//						}
//						if (!this.isDialogueShown()) { this.showDialogue(); }
//						this.setDialogueShown(true);	
//					} else {
//						this.setDialogueShown(false);
//					}
//					break;
//					
//				case UNICORN:
//		 		case ELVIRA:
//		 			
//		 			boolean playerHeal = false;
//		 			boolean elvira = this.getVillagerType() == VillagerType.ELVIRA;
//		 			
//		 			if (!player.isTeleporting() && player.isAlive() && (dist2 <= Unit.Squared(CLOSE_PROXIMITY))) {
//		 				String str = "";
//						if (player.getHP() < player.getMaxHP()) {
//							str = (elvira?"You're looking much healthier now.":"Neigh!");
//							playerHeal = true;
//						} else {
//							str = (elvira?"Return to me if you ever need healing.":"Neigh!");
//							player.setAddonStatus(false);
//						}
//						this.healUnit(player, delta);
//						this.setDialogue(str);
//						if (!this.isDialogueShown()) { this.showDialogue(); }
//						this.setDialogueShown(true);
//					} else {
//						player.setAddonStatus(false);
//						this.setDialogueShown(false);
//					}
//		 			
//		 			if (!playerHeal) {
//		 				
//		 				dist2 = Unit.Squared(CLOSE_PROXIMITY + VISIBLITY);
//		 				Ally ally = null;
//
//						for (Ally a : world.getAllies()) {
//							if (a.isAlive()) {
//								double d = calculateDistanceSqrd(a.getX(), a.getY(), this.getX(), this.getY());
//								a.setAddonStatus(false);
//								if (d < dist2) {
//									dist2 = d;
//									ally = a;
//								}
//							}
//						}
//		 				
//		 				if (dist2 <= Unit.Squared(CLOSE_PROXIMITY) && ally.isAlive()) {
//							if (ally.getHP() >= ally.getMaxHP()) {
//								ally.setAddonStatus(false);
//							}
//							this.healUnit(ally, delta);	
//						} else {
//							if (ally != null) { ally.setAddonStatus(false); }
//						}
//		 				
//		 			}
//		 			
//					break;
//					
//		  		case GARTH:
//		  			if (dist2 <= Unit.Squared(CLOSE_PROXIMITY)) {
//		  				// TODO make them random
//		  				if (Item.searchItem(player, Item.ItemType.AMULET) == null) {
//							this.setDialogue("Find the Amulet of Vitality, across the river to the west."); 
//						} else if (Item.searchItem(player, Item.ItemType.SWORD) == null) {
//							this.setDialogue("Find the Sword of Strength - cross the river and back, on the east side.");
//						} else if (Item.searchItem(player, Item.ItemType.TOME) == null) {
//							this.setDialogue("Find the Tome of Agility, in the Land of Shadows.");
//						} else {
//							this.setDialogue("You have found all the treasure I know of.");
//						}
//						if (!this.isDialogueShown()) { this.showDialogue(); }
//						this.setDialogueShown(true);	
//					} else {
//						this.setDialogueShown(false);
//					}
//					break;    		
//			}
//     	
//    	} else {
//    		
//    		this.checkDieing(false);
//    		
//    	}
//    	
//    	this.doCommonUpdates(delta);
//        
//    }
//    
//    /** Heal a unit.
//     * @param unit The unit to be healed.
//     * @param delta Time passed since previous frame (milliseconds).
//     */
//    private void healUnit(Unit unit, int delta) {
//    	double dx = unit.getX()-this.getX();
//		if (!unit.isTeleporting()) {
//			unit.setHP(unit.getHP()+HEAL_RATE*delta);
//		}
//    	if (unit.getHP() >= unit.getMaxHP()) {
//    		unit.setHP(unit.getMaxHP());
//    		unit.setAddonStatus(false);
//    		if (dx<0) { this.lookLeft(); } else { this.lookRight(); }
//    	} else if ((this.getVillagerType() == VillagerType.ELVIRA)) {
//    		this.setDisplayAnimation(dx<0?this.getHealLeftAnimation():this.getHealRightAnimation());
//    		unit.setAddonAnimation(this.getHealUnitAnimation());
//    		unit.setAddonStatus(true);
//    	}
//    }
//    
//    /* Static Methods */
//    
//    /** Checks if the villager can heal another unit.
//     * @param villager The villager unit to be checked.
//     * @return Whether the villager can heal or not.
//     */
//    public static boolean isHealingVillager(CopyOfVillager villager) {
//    	return (villager.getVillagerType() == VillagerType.ELVIRA || villager.getVillagerType() == VillagerType.UNICORN);
//    }
//    
//    
//    /* Getter and Setters beyond this point. */
//    
//    /** Get the Villager type.
//     * @return The villager type.
//     */
//    public VillagerType getVillagerType() {
//		return this.type;
//	}
//
//    /** Set the Villager type.
//     * @param type The villager type.
//     */
//    private void setVillagerType(VillagerType type) {
//		this.type = type;
//	}
//
//	/** Get the healing animation (only for type ELVIRA).
//     * @return The animation.
//     */
//	private Animation getHealLeftAnimation() {
//		return this.healLeft;
//	}
//
//	/** Set the healing animation (only for type ELVIRA).
//     * @param dialogTimer The dialog timer value.
//     */
//	private void setHealLeftAnimation(Image image) {
//		this.healLeft = new Animation(new SpriteSheet(image,72,72),FRAME_SLOW_INTERVAL);
//		this.getHealLeftAnimation().setLooping(true);
//		this.getHealLeftAnimation().setAutoUpdate(false);
//	}
//	
//	/** Get the healing animation (only for type ELVIRA).
//     * @return The animation.
//     */
//	private Animation getHealRightAnimation() {
//		return this.healRight;
//	}
//
//	/** Set the healing animation (only for type ELVIRA).
//     * @param dialogTimer The dialog timer value.
//     */
//	private void setHealRightAnimation(Image image) {
//		this.healRight = new Animation(new SpriteSheet(image,72,72),FRAME_SLOW_INTERVAL);
//		this.getHealRightAnimation().setLooping(true);
//		this.getHealRightAnimation().setAutoUpdate(false);
//	}
//	
//	/** Get the healing animation for the unit.
//     * @return The animation.
//     */
//	private Animation getHealUnitAnimation() {
//		return this.healing;
//	}
//
//	/** Set the healing animation for.
//     * @param dialogTimer The dialog timer value.
//     */
//	private void setHealUnitAnimation(Image image) {
//		this.healing = new Animation(new SpriteSheet(image,72,72),FRAME_SLOW_INTERVAL);
//		this.getHealUnitAnimation().setLooping(true);
//		this.getHealUnitAnimation().setAutoUpdate(false);
//	}
//	    
//}
